const {
    Mysql
} = require('../tools');
module.exports = async ctx => {
    let { vid, status } = ctx.request.body;
    let { uid } = ctx.query
    if (!uid || !vid || !status) {
        ctx.state.data = {
            code: '002',
            msg: '参数确实'
        }
    }
    else {
        let sql = `SELECT insertCollect(${vid},${uid},${status})`;
        try {
            let res = await Mysql(sql);
            ctx.state.data = {
                code: '001',
                data: 'ok'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                msg: '内部错误'
            }
        }
    }
}

/**
 * @param {mysql 有则更新 无则更新函数}
DELIMITER $$
DROP FUNCTION IF EXISTS insertCollect$$
CREATE FUNCTION insertCollect(curr_vid int,curr_uid int,curr_status int) RETURNS varchar(50)
BEGIN
  DECLARE num int;
    SELECT count(*) INTO num FROM collectVideo where vid=curr_vid AND uid=curr_uid;
    IF(num=0)
        THEN
        INSERT INTO collectVideo(vid,uid,status) VALUES(curr_vid,curr_uid,curr_status);
    ELSE
        UPDATE collectVideo SET status = curr_status WHERE vid=curr_vid AND uid=curr_uid;
  END IF;
	RETURN 1;
END $$
DELIMITER ;
*/